//
//  MJRefreshBackNormalFooter.swift
//  OC-Swift
//
//  Created by 陆文宇 on 2017/2/15.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import UIKit

class MJRefreshBackNormalFooter: MJRefreshBackStateFooter {
    
    var activityIndicatorViewStyle: UIActivityIndicatorViewStyle = .gray {
        didSet {
            loadingView.activityIndicatorViewStyle = activityIndicatorViewStyle
            loadingView.removeFromSuperview()
            addSubview(loadingView)
            setNeedsLayout()
        }
    }
    lazy var arrowView: UIImageView = UIImageView(image: Bundle.arrowImage)
    lazy var loadingView: UIActivityIndicatorView = {
        let _loadingView: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .gray)
        _loadingView.hidesWhenStopped = true
        return _loadingView
    }()
    override var state: MJRefreshComponent.MJRefreshState {
        set {
            let oldState: MJRefreshState = super.state
            if super.state != newValue {
                super.state = newValue
            }
            
            if state == .idle {
                if oldState == .refreshing {
                    arrowView.transform = CGAffineTransform(rotationAngle: 0.000001 - CGFloat.pi)
                    UIView.animate(withDuration: MJRefreshSlowAnimationDuration, animations: { 
                        self.loadingView.alpha = 0
                    }, completion: { (_) in
                        self.loadingView.alpha = 1
                        self.loadingView.stopAnimating()
                        
                        self.arrowView.isHidden = false
                    })
                } else {
                    arrowView.isHidden = false
                    loadingView.stopAnimating()
                    UIView.animate(withDuration: MJRefreshFastAnimationDuration, animations: { 
                        self.arrowView.transform = CGAffineTransform(rotationAngle: 0.000001 - CGFloat.pi)
                    })
                }
            } else if state == .pulling {
                arrowView.isHidden = false
                loadingView.stopAnimating()
                UIView.animate(withDuration: MJRefreshFastAnimationDuration, animations: { 
                    self.arrowView.transform = CGAffineTransform.identity
                })
            } else if state == .refreshing {
                arrowView.isHidden = true
                loadingView.startAnimating()
            } else if state == .noMoreData {
                arrowView.isHidden = true
                loadingView.stopAnimating()
            }
        }
        
        get {
            return _state
        }
    }
    override init(refreshingClosure: @escaping MJRefreshComponentRefreshingClosure) {
        super.init(refreshingClosure: refreshingClosure)
        
        addSubview(loadingView)
        addSubview(arrowView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func prepare() {
        super.prepare()
    }
    
    override func placeSubviews() {
        super.placeSubviews()
        
        var arrowCenterX: CGFloat = mj_w * 0.5
        if !stateLabel.isHidden {
            arrowCenterX -= labelLeftInset + stateLabel.mj_textWith * 0.5
        }
        let arrowCenterY = mj_h * 0.5
        let arrowCenter: CGPoint = CGPoint(x: arrowCenterX, y: arrowCenterY)
        
        if arrowView.constraints.count == 0 {
            arrowView.mj_size = arrowView.image!.size
            arrowView.center = arrowCenter
        }
        
        if loadingView.constraints.count == 0 {
            loadingView.center = arrowCenter
        }
        
        arrowView.tintColor = stateLabel.textColor
    }
    
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
